﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class VanningDA : IVanningDA
    {
        private string CREATED_BY = "@CREATED_BY";
        private string EXTERNAL_ORDER_ID = "@EXTERNAL_ORDER_ID";
        private string IS_ACTIVE = "@IS_ACTIVE";
        private string ORDER_ID = "@ORDER_ID";
        private string ORDER_TYPE = "@ORDER_TYPE";
        private string REMARK = "@REMARK";
        private string SQL_CHECK_VANNING_ID_UNIQUE = " SELECT COUNT(1) FROM @_@VANNING WHERE VANNING_ID = @VANNING_ID ";
        private string SQL_DELETE_VANNING = " DELETE FROM @_@VANNING WHERE VANNING_ID = @VANNING_ID ";
        private string SQL_INSERT_VANNING = " INSERT INTO @_@VANNING ( WH_ID, VANNING_ID, ORDER_ID, ORDER_TYPE, EXTERNAL_ORDER_ID, STATUS, REMARK, VANNING_TYPE, VANNING_DATE, IS_ACTIVE, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @VANNING_ID, @ORDER_ID, @ORDER_TYPE, @EXTERNAL_ORDER_ID, @STATUS, @REMARK, @VANNING_TYPE, @VANNING_DATE, @IS_ACTIVE, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_VANNING = " SELECT  V.ROWID, V.WH_ID, V.VANNING_ID, V.ORDER_ID, V.ORDER_TYPE, V.EXTERNAL_ORDER_ID, V.STATUS, V.REMARK, V.VANNING_TYPE, V.VANNING_DATE, V.IS_ACTIVE, V.TS, V.CREATED_BY, V.CREATED_DATE, V.UPDATED_BY, V.UPDATED_DATE  ,   T.[TEXT] as ORDER_TYPE_D  FROM  @_@VANNING V  LEFT JOIN @_@SYS_CODE_DETAIL SCD ON SCD.SYS_CODE = V.ORDER_TYPE AND SCD.SYS_CODE_TYPE = 'DOC_TYPE'   LEFT JOIN sysadm.TRANSLATION T ON 'DOC_TYPE.'+V.ORDER_TYPE = T.ITEM_ID   AND  T.ITEM_TYPE = 'SYS_CODE_DETAIL' AND T.LANG+'-'+T.COUNTRY = @LANGUAGE ";
        private string SQL_UPDATE_VANNING = " UPDATE @_@VANNING SET WH_ID = @WH_ID, ORDER_ID = @ORDER_ID, ORDER_TYPE = @ORDER_TYPE, EXTERNAL_ORDER_ID = @EXTERNAL_ORDER_ID, STATUS = @STATUS, REMARK = @REMARK, VANNING_TYPE = @VANNING_TYPE, VANNING_DATE = @VANNING_DATE, IS_ACTIVE = @IS_ACTIVE, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE VANNING_ID = @VANNING_ID  ";
        private string STATUS = "@STATUS";
        private string TS = "@TS";
        private string UPDATED_BY = "@UPDATED_BY";
        private string VANNING_DATE = "@VANNING_DATE";
        private string VANNING_ID = "@VANNING_ID";
        private string VANNING_TYPE = "@VANNING_TYPE";
        private string WH_ID = "@WH_ID";

        public bool CheckVanningIDUnique(string vanningID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@VANNING_ID", vanningID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_VANNING_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckVanningIDUnique(DataBase dataBase, DbTransaction tran, string vanningID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@VANNING_ID", vanningID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_VANNING_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteVanning(List<string> vanningID, string whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int j = 0; j < vanningID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@VANNING_ID", vanningID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_VANNING.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteVanning(DataBase dataBase, DbTransaction tran, List<string> vanningID, string whLoginID)
        {
            int result = 0;
            try
            {
                for (int j = 0; j < vanningID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@VANNING_ID", vanningID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_VANNING.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllVanning(string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_VANNING.Replace("@_@", whLoginID);
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            return DBHelper.ExecuteDataSet(CommandType.Text, sql, paras);
        }

        public DataSet GetAllVanning(DataBase dataBase, DbTransaction tran, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_VANNING.Replace("@_@", whLoginID);
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql, paras);
        }

        public VanningInfo GetVanningByID(string vanningID, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_VANNING.Replace("@_@", whLoginID) + " WHERE VANNING_ID = @VANNING_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@VANNING_ID", vanningID), new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            VanningInfo vanningInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    vanningInfo = new VanningInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["VANNING_ID"].ToString(), reader["ORDER_ID"].ToString(), reader["ORDER_TYPE"].ToString(), reader["EXTERNAL_ORDER_ID"].ToString(), reader["STATUS"].ToString(), reader["REMARK"].ToString(), reader["VANNING_TYPE"].ToString(), reader["VANNING_DATE"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return vanningInfo;
        }

        public VanningInfo GetVanningByID(DataBase dataBase, DbTransaction tran, string vanningID, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_VANNING.Replace("@_@", whLoginID) + " WHERE VANNING_ID = @VANNING_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@VANNING_ID", vanningID), new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            VanningInfo vanningInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                vanningInfo = new VanningInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["VANNING_ID"].ToString(), reader["ORDER_ID"].ToString(), reader["ORDER_TYPE"].ToString(), reader["EXTERNAL_ORDER_ID"].ToString(), reader["STATUS"].ToString(), reader["REMARK"].ToString(), reader["VANNING_TYPE"].ToString(), reader["VANNING_DATE"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return vanningInfo;
        }

        public DataSet GetVanningByQueryList(List<string> sqlWhere, VanningQueryEntity vanningQuery, string whLoginID, PubParasInfo pubParasInfo)
        {
            string temp = this.SQL_SELECT_ALL_VANNING.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!vanningQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, vanningQuery.CurrentPage, vanningQuery.PageSize, vanningQuery.SortField, vanningQuery.SortDirection).Replace("@LANGUAGE", "''" + pubParasInfo.UserLanguage + "''");
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetVanningByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, VanningQueryEntity vanningQuery, string whLoginID, PubParasInfo pubParasInfo)
        {
            string temp = this.SQL_SELECT_ALL_VANNING.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!vanningQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, vanningQuery.CurrentPage, vanningQuery.PageSize, vanningQuery.SortField, vanningQuery.SortDirection);
            }
            temp = temp.Replace("@LANGUAGE", "''" + pubParasInfo.UserLanguage + "''");
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public VanningInfo GetVanningByWhere(string where, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_VANNING.Replace("@_@", whLoginID) + where;
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            VanningInfo vanningInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    vanningInfo = new VanningInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["VANNING_ID"].ToString(), reader["ORDER_ID"].ToString(), reader["ORDER_TYPE"].ToString(), reader["EXTERNAL_ORDER_ID"].ToString(), reader["STATUS"].ToString(), reader["REMARK"].ToString(), reader["VANNING_TYPE"].ToString(), reader["VANNING_DATE"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return vanningInfo;
        }

        public int InsertVanning(VanningInfo vanningInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                vanningInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_Vanning_Parameters(vanningInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_VANNING.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertVanning(DataBase dataBase, DbTransaction tran, VanningInfo vanningInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                vanningInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_Vanning_Parameters(vanningInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_VANNING.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_Vanning_Parameters(VanningInfo vanningInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.VANNING_ID, SqlDbType.VarChar, 50), new SqlParameter(this.ORDER_ID, SqlDbType.VarChar, 50), new SqlParameter(this.ORDER_TYPE, SqlDbType.VarChar, 50), new SqlParameter(this.EXTERNAL_ORDER_ID, SqlDbType.VarChar, 50), new SqlParameter(this.STATUS, SqlDbType.VarChar, 50), new SqlParameter(this.REMARK, SqlDbType.VarChar, 300), new SqlParameter(this.VANNING_TYPE, SqlDbType.VarChar, 50), new SqlParameter(this.VANNING_DATE, SqlDbType.VarChar, 50), new SqlParameter(this.IS_ACTIVE, SqlDbType.VarChar, 1), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 30), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 30) };
            if (!string.IsNullOrEmpty(vanningInfo.WhID))
            {
                paramArray[0].Value = vanningInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = vanningInfo.VanningID;
            paramArray[2].Value = vanningInfo.OrderID;
            paramArray[3].Value = vanningInfo.OrderType;
            if (!string.IsNullOrEmpty(vanningInfo.ExternalOrderID))
            {
                paramArray[4].Value = vanningInfo.ExternalOrderID;
            }
            else
            {
                paramArray[4].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(vanningInfo.Status))
            {
                paramArray[5].Value = vanningInfo.Status;
            }
            else
            {
                paramArray[5].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(vanningInfo.Remark))
            {
                paramArray[6].Value = vanningInfo.Remark;
            }
            else
            {
                paramArray[6].Value = DBNull.Value;
            }
            paramArray[7].Value = vanningInfo.VanningType;
            if (!string.IsNullOrEmpty(vanningInfo.VanningDate))
            {
                paramArray[8].Value = vanningInfo.VanningDate;
            }
            else
            {
                paramArray[8].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(vanningInfo.IsActive))
            {
                paramArray[9].Value = vanningInfo.IsActive;
            }
            else
            {
                paramArray[9].Value = DBNull.Value;
            }
            paramArray[10].Value = vanningInfo.CreatedBy;
            paramArray[11].Value = vanningInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdateVanning(VanningInfo vanningInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                vanningInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_Vanning_Parameters(vanningInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_VANNING.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateVanning(DataBase dataBase, DbTransaction tran, VanningInfo vanningInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                vanningInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_Vanning_Parameters(vanningInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_VANNING.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

